$(document).ajaxSend(function(event, xhr, options){
    xhr.setRequestHeader("user_token", localStorage.getItem("token"))
})

$(document).ajaxError(function(event, xhr, options, exc){
    if(xhr.status == 401){
        alert("登录已过期，请重新登录")
        localStorage.removeItem("token")
        localStorage.removeItem("userId")
        location.href = "blog_login.html"
    }
})

function getUserInfo(url) {
    $.ajax({
        url: url,
        type: 'GET',
        success: function (res) {
            if (res != null && res.code == 'SUCCESS') {
                let userInfo = res.data
                $(".left .card h3").text(userInfo.userName)
                $(".left .card a").attr("href", userInfo.githubUrl)
            }
        },
        error: function (err) {
            console.log(err)
        }
    })
}

function logout() {
    let result = confirm("确定要退出登录吗？")
    if (result) {
        localStorage.removeItem("token")
        localStorage.removeItem("userId")
        location.href = "blog_login.html"
    }
}